package com.gehj.androidlibs;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.multidex.MultiDex;

import com.gehj.alluitls.FileStorageManger;
import com.gehj.alluitls.androidUtils.LogUtil;
import com.gehj.base.broadcast.NetChangeReceiver;
import com.gehj.uhttp.OkhttpUtils;
import com.gehj.uhttp.download.HttpDownLoaderManger;

import java.util.ArrayList;
import java.util.List;

/**
  *
  * @ClassName:      MyApplication
  * @Description:
 * 主要功能： 1. 退出所有activity
 * 3.没有网络处理 fixme 可以移到BaseActivity中处理 --> 添加snakerbar进行提醒
 * 4.方法数超过65535处理
 * https://www.jianshu.com/p/78f2e2d9484a
 * https://developer.android.google.cn/studio/build/multidex?hl=zh-cn#java
 *
 * 5.各种初始化 TODO 后期进行启动优化
 *
 * 6. TODO 学习R8缩小apk体积
 * https://developer.android.google.cn/studio/build/shrink-code?hl=zh-cn
 *
  * @Author:         gehj
  * @CreateDate:     2020/12/15 14:36
 */

public class MyApplication extends Application {

    //activity列表
    private static List<Activity> activities = new ArrayList<>(); //退出所有activity
    private Handler handler;
    private static Context context;
    @Override
    public void onCreate() {
        super.onCreate();
        LogUtil.isLogEnabled = true; //IMPORTANT:正式包的时候变成false;
        //FileStorageManger.getInstance().init(this);//初始化文件管理;//TODO 这个工具类需要重写；
        HttpDownLoaderManger.getInstance().init(this); //下载文件;
        OkhttpUtils.init(this);

        context = getApplicationContext(); //获取全局context

        //网络中断的变化
        if (!NetChangeReceiver.isNetworkAvailable(this)) {
            Toast.makeText(this, "网络无连接,请检查相关配置", Toast.LENGTH_SHORT).show();
        }

    }

    public static Context getContext(){
        return context;
    }

    //启动方法限制65535
    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);

        MultiDex.install(this);
    }


    private ActivityLifecycleCallbacks callbacks = new ActivityLifecycleCallbacks() {
        @Override
        public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle savedInstanceState) {
            Looper mainLooper = getMainLooper();
            // 获取主线程的handler
             handler = new Handler(mainLooper);
             handler.post(new Runnable() {
                 @Override
                 public void run() {
                     //list中添加activity
                     activities.add(activity);
                 }
             });
        }

        @Override

        public void onActivityStarted(@NonNull Activity activity) {

        }

        @Override
        public void onActivityResumed(@NonNull Activity activity) {

        }

        @Override
        public void onActivityPaused(@NonNull Activity activity) {

        }

        @Override
        public void onActivityStopped(@NonNull Activity activity) {

        }

        @Override
        public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle outState) {

        }

        @Override
        public void onActivityDestroyed(@NonNull Activity activity) {
                activities.remove(activity);

        }
    };


    /**
     * 退出所有activity(此栈中)
     * 其他地方用
     */
    public static void exitAllActivitis() {

        for (int i = activities.size() - 1; i >= 0; i--) {
            Activity activity = activities.get(i);
            if (activity != null) {
                activity.finish();
            }
        }
    }



}
